﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using MTS.Api;
using MTS.Hubs;
using MTS.Services;

namespace MTS.Hosting.IIS
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddTransient<ICustomerService, CustomerService>();
            services.AddTransient<IOrderService, OrderService>();
            services.AddTransient<IScoreService, ScoreService>();

            services.AddSockets();
            services.AddSignalR();

            services.AddCors(o =>
            {
                o.AddPolicy("Everything", p =>
                {
                    p.AllowAnyHeader()
                        .AllowAnyMethod()
                        .AllowAnyOrigin();
                });
            });

            //services.AddEndPoint<MessagesEndPoint>();
        }
        
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            app.UseFileServer();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseCors("Everything");

            app.UseSignalR(routes =>
            {
                routes.MapHub<OrderHub>("orders");
                routes.MapHub<ScoreHub>("scores");
                routes.MapHub<CustomerHub>("customers");
            });

            //app.UseSockets(routes =>
            //{
            //    routes.MapEndPoint<MessagesEndPoint>("chat");
            //});
        }
    }
}
